package com.victualler.core.base.service.impl;

import com.victualler.core.base.dao.BaseDetailDao;
import com.victualler.core.base.repository.BaseDetailRepository;
import com.victualler.core.base.repository.IBaseQueryRepository;
import com.victualler.core.base.service.BaseDetailService;

import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 18:04 2018/4/5
 * @Discription:    通用的实体类服务类  根据实体类型封装基本的CRUD方法（不含分页）
 */
public  abstract class BaseDetailServiceImpl<E extends BaseDetailDao,R extends BaseDetailRepository<E,Long>,QR extends IBaseQueryRepository<E,Long>> extends BaseServiceImpl<E,R,QR>  implements BaseDetailService<E,R,QR> {
    @Override
    public List<E> getByParentId(Long parentId) {
        return this.baseRepository.findByParentId(parentId);
    }

    @Override
    public void deleteByParentId(Long parentId) {
        this.baseRepository.deleteByParentId(parentId);
    }

    /**
     * @Aouthor Guoshichang
     * @Date 16:49 on 2018/6/15.
     * @param newDaoList
     * @Function 根据新的列表增减其他列表
     */
    @Override
    public void updateList(List<E> newDaoList,Long parentId){
        List<E> oldDaoList = this.baseRepository.findByParentId(parentId);
        //删掉重复
        oldDaoList.forEach(item->{
            boolean find = false;
            for (E goodsImgDetailDao : newDaoList) {
                if(goodsImgDetailDao.getId()!=null && item.getId() == goodsImgDetailDao.getId()){
                    find=true;
                    break;
                }
            }
            //没找到就删掉
            if(find == false){
                this.baseRepository.delete(item.getId());
            }
        });
        //更新父id
        newDaoList.forEach(item->{
            if(item.getId() == null){
                item.setParentId(parentId);
            }
        });
        this.baseRepository.save(newDaoList);
    }

}
